const url = require('../../utils/url')
// pages/privacySetting/privacySetting.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked: true,
  },
  // 公开借阅信息
  publicborrowinfo() {
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '加载中',
    })
    const that = this;
    wx.request({
      url: getApp().url.dev + '/api/user/publicBorrowInfo',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        token: wx.getStorageSync('token')
      },
      data: {
        bookInfo: that.data.switch1Checked
      },
      success(res) {
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
        wx.hideLoading()
        if(res.data.code == 200){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          
          // that.getUserInfo();//获取用户信息接口
        }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      } 
    })
  },


   // 获取用户信息
   getUserInfo:function(){
    var that = this;
    wx.request({
      url: getApp().url.dev + '/api/user/getUserInfo', //这里就写上后台解析手机号的接口
      //这里的几个参数是获取授权后的加密数据，作为参数传递给后台就行了
      data: {},
      method: 'get',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
         'token': wx.getStorageSync("token"),
      },
      success(res) {
        console.log(res);
        if(res.data.code == 200){
          that.setData({
            switch1Checked:res.data.data.bookInfo
          })
        }
      }
    })
  }, 

  onLoad: function (options) {
    const that = this;
    this.getUserInfo();//获取用户信息
  },


  switch1Change: function (e) {
    const that = this;
    that.setData({
      switch1Checked: e.detail.value
    })
    that.publicborrowinfo();
  },

})